﻿using System;
using System.Collections.Generic;


namespace HEFramework
{
    /// <summary>
    ///
    /// 资源加载
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-12-21 14:19:44
    /// ----------------------------------------
    /// </summary>
    public class AssetLoader : Singleton<AssetLoader>, ISingletonAwake, ISingletonUpdate
    {
        private List<Assets> assets = new();

        private IAssetLoad assetLoad;

        public void Awake()
        {
            switch (AssetBundleSettingScriptableObject.Instance.Domain)
            {
                case eDomain.Unity:
                    assetLoad = new Unity_AssetLoad();
                    break;
                case eDomain.Resources:
                    assetLoad = new Resources_AssetLoad();
                    break;
                case eDomain.Streaming:
                    assetLoad = new Streaming_AssetLoad();
                    break;
                case eDomain.Persistent:
                    assetLoad = new Persistent_AssetLoad();
                    break;
            }
        }


        public void Update()
        {
            for (int i = 0; i < assets.Count; i++)
            {
                assets[i].Update();
            }
        }

        protected override void Destroy()
        {
            for (int i = 0; i < assets.Count; i++)
            {
                assets[i].Dispose();
            }

            assets.Clear();
        }

        /// <summary>
        /// 获取资源
        /// </summary>
        public Assets GetAssets()
        {
            Assets assets = ObjectGenerater.Instance.Fetch<Assets>();
            this.assets.Add(assets);
            return assets;
        }

        /// <summary>
        /// 移除资源
        /// </summary>
        /// <param name="_assets"></param>
        public void RemoveAssets(Assets _assets)
        {
            assets.Remove(_assets);
        }

        /// <summary>
        /// 读取资源（同步）
        /// </summary>
        /// <param name="_assetBundleName">Ab包名称</param>
        /// <param name="_assetName">Ab包内读取的资源名称</param>
        /// <param name="_extension">读取的资源类型后缀</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T LoadAssetSyn<T>(string _assetBundleName, string _assetName, string _extension) where T : UnityEngine.Object
        {
            return assetLoad.LoadAssetSyn<T>(_assetBundleName, _assetName, _extension);
        }


        /// <summary>
        /// 读取资源（异步）
        /// </summary>
        /// <param name="_assetBundleName">Ab包名称</param>
        /// <param name="_assetName">Ab包内读取的资源名称</param>
        /// <param name="_extension">读取的资源类型后缀</param>
        /// <param name="_callback">数据接收回调函数</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public void LoadAssetAsync<T>(string _assetBundleName, string _assetName, string _extension, Action<T> _callback) where T : UnityEngine.Object
        {
            if (_callback == null)
            {
                return;
            }

            assetLoad.LoadAssetAsync(_assetBundleName, _assetName, _extension, _callback);
        }


        /// <summary>
        /// 卸载资源（异步）
        /// </summary>
        /// <param name="_assetBundleName">AB包名称</param>
        public void UnLoadAssetAsync(string _assetBundleName)
        {
            assetLoad.UnLoadAssetAsync(_assetBundleName);
        }
    }
}